package com.bafa.dbcache;

import java.io.StringReader;
import java.sql.ResultSet;
import java.util.TreeMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.bafa.jdbc.DBAccess;
import com.bafa.jdbc.QueryObject;
import com.bafa.middle.betwixt.Utils;

import com.bafa.objects.team.Team;


public class GrabTeams {

	private static TreeMap<Integer,Team> teams = new TreeMap();
    private static TreeMap<String,Team> teamsByName = new TreeMap();
	private static int latestTeamId = 0;
	public static void grabTeams(DBAccess db)throws Exception{
		//*
		 try{
		 
		         ResultSet teamsResult = db.retrieveTeams();
		         
		         while(teamsResult.next()){
		                  try{
		              
		                
		               Team team = (Team)Utils.readBetwixt(new String(teamsResult.getBytes("team")),"team",Team.class);
					   //System.out.println(team.getId());
		                       teamsByName.put(team.getName().toUpperCase(),team);
		                      teams.put(team.getId(), team);
							  if(team.getId()>latestTeamId)
								  latestTeamId = team.getId();
		                  }catch(Exception ioe){
		                     ioe.printStackTrace();
		                 }
		                 
		         }
				 
		         
		 }catch(Exception e){
		         e.printStackTrace();
		 }
                 
		//*/
		
		/*
		QueryObject teamsQuery = new QueryObject("teams");
		ResultSet teamsResult = db.query(teamsQuery);
		
		
		
		while(teamsResult.next()){
			int teamId = teamsResult.getInt("ID");
			ObjectFactory factory=new ObjectFactory();
			Team team = factory.createTeam();
			TeamDetails teamDetails = factory.createTeamDetails();
			teamDetails.setID(teamId);
			teamDetails.setName(teamsResult.getString("Name"));
			teamDetails.setContactName1(teamsResult.getString("COntactName1"));
			teamDetails.setContactName2(teamsResult.getString("COntactName2"));
			teamDetails.setContactEmail1(teamsResult.getString("COntactEmail1"));
			teamDetails.setContactEmail2(teamsResult.getString("COntactEmail2"));
			teamDetails.setContactTel1(teamsResult.getString("COntactTel1"));
			teamDetails.setContactTel2(teamsResult.getString("COntactTel2"));
			teamDetails.setHomeShirtColour(teamsResult.getString("ShirtColour"));
			teamDetails.setPaymentRequired(teamsResult.getDouble("PaymentRequired"));
		
			team.setTeamDetails(teamDetails);
			// Get Stats
			GrabTeamStats.grabTeamStats(db,team);
				
			
			teams.put(teamId,team);
			db.storeTeam(team);
			
		}//*/
		
	}
	
	public static void addTeam(Team team){
		teams.put(team.getId(), team);
	}
	static ResultSet grabTeam(DBAccess db,int teamId)throws Exception{
		QueryObject teamsQuery = new QueryObject("teams");
		teamsQuery.addWhereAndField("id",teamId+"");
		return db.query(teamsQuery);
	}
	/*static void grabTeam(DBAccess db,int teamId, int seasonId)throws Exception{
		
		ResultSet teamsResult = grabTeam(db,teamId);
		
		while(teamsResult.next()){
			ObjectFactory factory=new ObjectFactory();
			Team team = factory.createTeam();
			TeamDetails teamDetails = factory.createTeamDetails();
			teamDetails.setID(teamId);
			teamDetails.setName(teamsResult.getString("Name"));
			teamDetails.setContactName1(teamsResult.getString("COntactName1"));
			teamDetails.setContactName2(teamsResult.getString("COntactName2"));
			teamDetails.setContactEmail1(teamsResult.getString("COntactEmail1"));
			teamDetails.setContactEmail2(teamsResult.getString("COntactEmail2"));
			teamDetails.setContactTel1(teamsResult.getString("COntactTel1"));
			teamDetails.setContactTel2(teamsResult.getString("COntactTel2"));
			teamDetails.setHomeShirtColour(teamsResult.getString("ShirtColour"));
			teamDetails.setPaymentRequired(teamsResult.getDouble("PaymentRequired"));
		
			//// Get Details
			//Team team = new Team(teamId,teamsResult.getString("Name"),teamsResult.getString("ContactName1"),teamsResult.getString("ContactName2"),teamsResult.getString("COntactEmail1"),teamsResult.getString("COntactEmail2"),teamsResult.getString("COntactTel1"),teamsResult.getString("COntactTel2"),teamsResult.getString("ShirtColour"),teamsResult.getDouble("PaymentRequired"));
			
			// Get Stats
			GrabTeamStats.grabTeamStats(db,team,seasonId);
			
			teams.put(teamId,team);
		}
		
		
	}
	*
	static TeamDetails grabTeamDetails(DBAccess db,int teamId)throws Exception{
		
		ResultSet teamsResult = grabTeam(db,teamId);
		TeamDetails teamDetails = null;
		while(teamsResult.next()){
			ObjectFactory factory=new ObjectFactory();
			Team team = factory.createTeam();
			teamDetails = factory.createTeamDetails();
			teamDetails.setID(teamId);
			teamDetails.setName(teamsResult.getString("Name"));
			teamDetails.setContactName1(teamsResult.getString("COntactName1"));
			teamDetails.setContactName2(teamsResult.getString("COntactName2"));
			teamDetails.setContactEmail1(teamsResult.getString("COntactEmail1"));
			teamDetails.setContactEmail2(teamsResult.getString("COntactEmail2"));
			teamDetails.setContactTel1(teamsResult.getString("COntactTel1"));
			teamDetails.setContactTel2(teamsResult.getString("COntactTel2"));
			teamDetails.setHomeShirtColour(teamsResult.getString("ShirtColour"));
			teamDetails.setPaymentRequired(teamsResult.getDouble("PaymentRequired"));
		
			//// Get Details
			//details = new TeamDetails(teamsResult.getString("Name"),teamsResult.getString("ContactName1"),teamsResult.getString("ContactName2"),teamsResult.getString("COntactEmail1"),teamsResult.getString("COntactEmail2"),teamsResult.getString("COntactTel1"),teamsResult.getString("COntactTel2"),teamsResult.getString("ShirtColour"),teamsResult.getDouble("PaymentRequired"));
			
			
		}
		return teamDetails;
		
		
	}
	*/
	public static TreeMap getTeams() {
		return teams;
	}

    public void setTeamsByName(TreeMap<String, Team> teamsByName) {
        this.teamsByName = teamsByName;
    }

    public static TreeMap<String, Team> getTeamsByName() {
        return teamsByName;
    }

	public static int getLatestTeamId() {
		return latestTeamId;
	}

	public static void setLatestTeamId(int latestTeamId) {
		GrabTeams.latestTeamId = latestTeamId;
	}
}
